/**
 * 初始化页面
 */
$(function () {
    var id = sessionStorage.getItem('aId');
    // console.log(id);
    //发异步，从Java获取数据
    $.ajax({
        type: 'get',
        url: requestURL+prefix+userService+'/admin/findById/'+id,
        headers: {"Authorization":authToken},
        success: function (data) {
            if (data.message == "success") {
                //赋值
                $("#L_id").val(data.object.id);
                $("#L_name").val(data.object.name);
                $("#L_email").val(data.object.email);
                $("#birthday").val(data.object.birthday);
                $("#L_phone").val(data.object.phone);
                $("#L_site").val(data.object.site);
                // 获取到性别值进行匹配
                $("#sex :radio").each(function () {
                    var sex = $(this);
                    if (sex.val() == data.object.sex) {
                        sex.prop("checked",true);
                        layui.form.render();
                    }
                })
                return false;
            }
            // console.log(data);
            layer.msg('信息获取失败，请稍后重试！',{icon:0,time:1200});
            return false;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            var state = XMLHttpRequest.status;
            if (state == 403) {
                layui.layer.msg('权限不足，请联系超级管理员！', {icon: 0, time: 1200});
                return false;
            }
            layui.layer.msg('请求接口异常！', {icon: 0, time: 1200});
            return false;
        }
    });
});

layui.use(['form', 'layer', 'laydate', 'jquery'], function () {
    $ = layui.jquery;
    var form = layui.form,
        laydate = layui.laydate;
    layer = layui.layer;

    //执行一个laydate实例
    laydate.render({
        elem: '#birthday', //指定元素
        trigger: 'click' //采取事件弹出
    });

    //自定义验证规则
    form.verify({
        name: function (value) {
            if (value.length < 2) {
                return '昵称至少得2个字符';
            }
        },
    });

    //监听提交
    form.on('submit(add)', function (data) {
        console.log(data.field);
        //发异步，把数据提交给Java
        $.ajax({
            type: 'post',
            url: requestURL+prefix+userService+'/admin/updateMessage',
            data: JSON.stringify(data.field),
            contentType: "application/json;charset=utf-8",
            dataType: 'json',
            success: function (res) {
                if (res.status == 200) {
                    $("#sumbit").attr("disabled",true); //禁止点击按钮
                    layer.msg('修改成功！', {icon: 1, time: 1100,end:function () {
                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        }});
                    return false;
                }
                if (res.status == 403) {
                    layui.layer.msg('权限不足，请联系超级管理员！', {icon: 0, time: 1200});
                    return false;
                }
                layer.msg('修改失败，请稍后重试！', {icon: 2, time: 1200});
                return false;
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                var state = XMLHttpRequest.status;
                if (state == 403) {
                    layui.layer.msg('权限不足，请联系超级管理员！', {icon: 0, time: 1200});
                    return false;
                }
                layui.layer.msg('请求接口异常！', {icon: 0, time: 1200});
                return false;
            }

        });
        return false;
    });

});

/**
 * 重置管理员信息修改表单信息
 */
function resetAdminForm() {
    var id = sessionStorage.getItem('aId');
    // console.log(id);
    //发异步，从Java获取数据
    $.ajax({
        type: 'get',
        url: requestURL+prefix+userService+'/admin/findById/'+id,
        success: function (data) {
            if (data.status == 200 ) {
                //赋值
                $("#L_id").val(data.object.id);
                $("#L_name").val(data.object.name);
                $("#L_email").val(data.object.email);
                $("#birthday").val(data.object.birthday);
                $("#L_phone").val(data.object.phone);
                $("#L_site").val(data.object.site);
                // 获取到性别值进行匹配
                $("#sex :radio").each(function () {
                    var sex = $(this);
                    if (sex.val() == data.object.sex) {
                        sex.prop("checked",true);
                        layui.form.render();
                    }
                })
                return false;
            }
            // console.log(data);
            layer.msg('重置失败，请稍后重试！',{icon:0,time:1200});
            return false;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            var state = XMLHttpRequest.status;
            if (state == 403) {
                layui.layer.msg('权限不足，请联系超级管理员！', {icon: 0, time: 1200});
                return false;
            }
            layui.layer.msg('请求接口异常！', {icon: 0, time: 1200});
            return false;
        }
    });
}
